package org.schrag.sbbdroid;

import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.schrag.sbbdroid.data.Connection;
import org.schrag.sbbdroid.data.SBBResponse;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TimetableSearchActivity extends Activity implements OnClickListener {
	Button BSearch;
	Button BGR;
	
	EditText ETFrom;
	EditText ETTo;
	EditText ETVia;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.timetablesearch);
		
		BSearch = (Button) findViewById(R.id.BSearch);
		BSearch.setOnClickListener(this);
		
		BGR = (Button) findViewById(R.id.BGR);
		BGR.setOnClickListener(this);
		
		ETFrom = ((EditText) findViewById(R.id.ETFrom));
		ETTo = ((EditText) findViewById(R.id.ETTo));
		ETVia = ((EditText) findViewById(R.id.ETVia));
	}

	// Implement the OnClickListener callback
	public void onClick(View v) {
		SBBResponse myResponse = SBBResponseHandler.getResponse(
				ETFrom.getText().toString(),
				ETTo.getText().toString(),
				ETVia.getText().toString(), 
				new Date(), 
				true);
		AlertDialog.Builder builder=new AlertDialog.Builder(this);
		if (myResponse != null) {
			if (myResponse.getConnections() != null)
			{
				Intent i = new Intent(this, ConnectionList.class);
				i.putExtra(ConnectionList.CONNECTIONS, myResponse.getConnections());
				startActivity(i);				
			}
			else
			{
				builder
				.setTitle("Exception!")
				.setMessage("No Connections Found...")
				.setPositiveButton("OK", null)
				.show();
			}
			
		} else {
			builder
			.setTitle("Exception!")
			.setMessage("Error Downloading...")
			.setPositiveButton("OK", null)
			.show();
		}
	}
}